package com.rtx.edi.gatway.test;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.rtx.common.EdiConstant;
import com.rtx.edi.gatway.cache.EdiApiContext;
import com.rtx.edi.model.api.po.PlApiMapping;
import com.rtx.edi.platform.custom.bean.EdiCustomProcess;
import com.rtx.edi.platform.rule.bean.PlSerialRule;
import com.rtx.edi.platform.rule.bean.UpdRule;
import com.rtx.edi.platform.rule.bean.WhseidRule;
import com.rtx.edi.platform.tableconfig.bean.EdiColumnValueConfig;
import com.rtx.edi.platform.tableconfig.bean.EdiTableConfig;

public class InitHelp {

	public static void loadTestSkuConfig(EdiApiContext apiContext) {
		
		PlApiMapping apimapping = new PlApiMapping();
//		apiContext.setPlApiMapping(apimapping);
		
		EdiTableConfig ediTableConfig = new EdiTableConfig();
		ediTableConfig.setName("sku");
		ediTableConfig.setRequestrootnode(EdiConstant.REQUEST_ROOT_NODE);
		ediTableConfig.setUpdaterule("");
		
		List<EdiColumnValueConfig> columnvalueList = new ArrayList<EdiColumnValueConfig>();
		String cvcInit = "whseid|whseid,storerkey|storerkey,sku|sku,descr|descr,sourceversion|sourceversion";
		for(String data:cvcInit.split(",")) {
			String column = data.split("\\|")[0];
			String node = data.split("\\|")[1];
			EdiColumnValueConfig cvc = new EdiColumnValueConfig();
			if(Arrays.asList("storerkey,sku".split(",")).contains(column)) {
				cvc.setUpdcondition("true");
			}
			if(Arrays.asList("sourceversion".split(",")).contains(column)) {
				cvc.setUpdcolumn("true");
			}
			cvc.setColumnname(column);
			cvc.setRequestvaluepath(node);
			columnvalueList.add(cvc);
		}
		ediTableConfig.setColumnvalue(columnvalueList);
		
		apiContext.getEdiTableConfigList().add(ediTableConfig);
		
		WhseidRule  whseidRule  = new WhseidRule();
		whseidRule.setName("SKU下发仓库号确定规则");
		whseidRule.setBeanname("skuWhseidRule");
		apiContext.setWhseidRule(whseidRule);
		
		UpdRule updRule = new UpdRule();
		updRule.setName("sku下发更新规则");
		updRule.setExistquerysql("select count(1) from sku where sku ={sku} and storerkey ={storerkey}");
		updRule.setExistquerycolumns("sku,storerkey");
		apiContext.setUpdRule(updRule);
		
		EdiCustomProcess ediCustomProcess = new EdiCustomProcess();
		ediCustomProcess.setBeanname("skuValidate");
		ediCustomProcess.setName("sku自定义校验");
		ediCustomProcess.setPriority(10);
		ediCustomProcess.setType(EdiConstant.CUSTOM_PROCESS_VALIDATE);
		apiContext.getEdiCustomProcessList().add(ediCustomProcess);
		
		
		EdiCustomProcess ediCustomProcessAfter = new EdiCustomProcess();
		ediCustomProcessAfter.setBeanname("skuAfterHandle");
		ediCustomProcessAfter.setName("sku更新或插入后执行逻辑");
		ediCustomProcessAfter.setPriority(10);
		ediCustomProcessAfter.setType(EdiConstant.CUSTOM_PROCESS_CUSTOMAFTER);
		apiContext.getEdiCustomProcessList().add(ediCustomProcessAfter);
	}
	
	public static void loadTestOrdersConfig(EdiApiContext apiContext) {
		
		PlApiMapping apimapping = new PlApiMapping();
//		apiContext.setPlApiMapping(apimapping);
		
		EdiTableConfig ediTableConfig = new EdiTableConfig();
		ediTableConfig.setName("orders");
		ediTableConfig.setRequestrootnode(EdiConstant.REQUEST_ROOT_NODE);
		ediTableConfig.setUpdaterule("");
		
		List<EdiColumnValueConfig> columnvalueList = new ArrayList<EdiColumnValueConfig>();
		String cvcInit = "orderkey|orderkey,externorderkey|externorderkey,externorderkey2|rtxexternorderkey2,storerkey|storerkey,orderdate|orderdate,type|type";
		for(String data:cvcInit.split(",")) {
			String column = data.split("\\|")[0];
			String node = data.split("\\|")[1];
			EdiColumnValueConfig cvc = new EdiColumnValueConfig();
			if(Arrays.asList("externorderkey".split(",")).contains(column)) {
				cvc.setUpdcondition("true");
			}
			if(Arrays.asList("externorderkey2,orderdate,type".split(",")).contains(column)) {
				cvc.setUpdcolumn("true");
			}
			if(Arrays.asList("batchid".split(",")).contains(column)) {
//				cvc.setIsuuid("true");
			}
			if(Arrays.asList("orderdate".split(",")).contains(column)) {
				cvc.setDefaultvalue(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
			}
			if(Arrays.asList("orderkey".split(",")).contains(column)) {
				cvc.setIsseriakey("true");
			}
			cvc.setColumnname(column);
			cvc.setRequestvaluepath(node);
			columnvalueList.add(cvc);
		}
		ediTableConfig.setColumnvalue(columnvalueList);
		
		apiContext.getEdiTableConfigList().add(ediTableConfig);
		
		
		
		EdiTableConfig ediTableConfig2 = new EdiTableConfig();
		ediTableConfig2.setName("orderdetail");
		ediTableConfig2.setRequestrootnode("details");
		ediTableConfig2.setUpdaterule("");
		
		List<EdiColumnValueConfig> columnvalueList2 = new ArrayList<EdiColumnValueConfig>();
		String cvcInit2 = "orderlinenumber|orderlinenumber,orderkey|orderkey,externlineno|details数组.externlineno,externlineno2|details数组.rtxexternlineno2,storerkey|details数组.storerkey,sku|details数组.sku,originalqty|details数组.openqty";
		for(String data:cvcInit2.split(",")) {
			String column = data.split("\\|")[0];
			String node = data.split("\\|")[1];
			EdiColumnValueConfig cvc = new EdiColumnValueConfig();
			if(Arrays.asList("externorderkey".split(",")).contains(column)) {
				cvc.setUpdcondition("true");;
			}
			if(Arrays.asList("orderkey|orderkey,externlineno|externlineno,externlineno2|rtxexternlineno2,storerkey|storerkey,sku|sku,qty|originalqty".split(",")).contains(column)) {
				cvc.setUpdcolumn("true");
			}
			if(Arrays.asList("batchid".split(",")).contains(column)) {
//				cvc.setIsuuid("true");
			}
			if(Arrays.asList("orderkey".split(",")).contains(column)) {
				cvc.setIsseriakey("true");
			}
			if(Arrays.asList("orderlinenumber".split(",")).contains(column)) {
//				cvc.setIslinenumber("true");
			}
			cvc.setColumnname(column);
			cvc.setRequestvaluepath(node);
			columnvalueList2.add(cvc);
		}
		ediTableConfig2.setColumnvalue(columnvalueList2);
		
		apiContext.getEdiTableConfigList().add(ediTableConfig2);
		
		//流水号生成规则
		PlSerialRule plSerialRule = new PlSerialRule();
		plSerialRule.setCode("ORDERS");
//		plSerialRule.setCode("11703");
		plSerialRule.setType(EdiConstant.SERIAL_RULE_TYPE_NCOUNT);
		apiContext.setSerialRule(plSerialRule);
		
		WhseidRule  whseidRule  = new WhseidRule();
		whseidRule.setName("orders下发仓库号确定规则");
		whseidRule.setBeanname("ordersWhseidRule");
		apiContext.setWhseidRule(whseidRule);
		
		UpdRule updRule = new UpdRule();
		updRule.setName("orders下发更新规则");
		updRule.setExistquerysql("select count(1) from orders where externorderkey ={externorderkey}");
		updRule.setExistquerycolumns("externorderkey");
		apiContext.setUpdRule(updRule);
		
		EdiCustomProcess ediCustomProcess = new EdiCustomProcess();
		ediCustomProcess.setBeanname("orderValidate");
		ediCustomProcess.setName("orders自定义校验");
		ediCustomProcess.setPriority(10);
		ediCustomProcess.setType(EdiConstant.CUSTOM_PROCESS_VALIDATE);
		apiContext.getEdiCustomProcessList().add(ediCustomProcess);
		
		
		EdiCustomProcess ediCustomProcessAfter = new EdiCustomProcess();
		ediCustomProcessAfter.setBeanname("ordersAfterHandle");
		ediCustomProcessAfter.setName("orders更新或插入后执行逻辑");
		ediCustomProcessAfter.setPriority(10);
		ediCustomProcessAfter.setType(EdiConstant.CUSTOM_PROCESS_CUSTOMAFTER);
		apiContext.getEdiCustomProcessList().add(ediCustomProcessAfter);
	}
}
